package sse.ustc.cc.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Path;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class CustomDayView extends View implements View.OnTouchListener {
	private OnItemClickListener onItemClickListener;
	private Surface surface;
	
	public CustomDayView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		// TODO Auto-generated constructor stub
	}

	private void init() {
		// TODO Auto-generated method stub
		surface = new Surface();
		surface.density = getResources().getDisplayMetrics().density;
		setBackgroundColor(surface.bgColor);
		setOnTouchListener(this);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		// 画框
		canvas.drawPath(surface.boxPath, surface.borderPaint);
		
		//时间
		float weekTextX = surface.timeWidth * 3/4f;
		// 星期背景
		for (int i = 0; i < surface.time.length; i++) {
			float weekTextY = i
					* surface.timeHeight
					+ (surface.timeWidth - surface.TimePaint
							.measureText(surface.time[i])) / 2f;
			canvas.drawText(surface.time[i], weekTextX, weekTextY,
					surface.TimePaint);
		}
		
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		// TODO Auto-generated method stub
		if (changed) {
			surface.init();
		}
		super.onLayout(changed, left, top, right, bottom);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		surface.width = getResources().getDisplayMetrics().widthPixels;
		surface.height =(int)(getResources().getDisplayMetrics().heightPixels)*7/9;

		widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(surface.width,
				View.MeasureSpec.EXACTLY);
		heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(surface.height,
				View.MeasureSpec.EXACTLY);
		setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
		this.onItemClickListener = onItemClickListener;
	}
	
	class Surface {
		public float density;
		public int width; // 整个控件的宽度
		public int height; // 整个控件的高度
		public float cellWidth; // 日期方框宽度
		public float cellHeight; // 日期方框高度

		
		public float borderWidth;
		public int bgColor = Color.parseColor("#ffffff");  //方框的背景颜色
		private int textColor = Color.BLACK;
		private int borderColor = Color.parseColor("#CCCCCC");   
		
		public int cellSelectedColor = Color.parseColor("#99CCFF");
		public Path boxPath; // 边框路径
		
		public String[] time = {"上午", "下午", "晚上"};
		public String[] jieshu = { "第一节","第二节", "第三节", "第四节", "第五节", "第六节",
									"第七节", "第八节","第九节","第十节"};
		private Paint borderPaint;
		private Paint TimePaint;
		private Paint jieshuPaint;
		private float timeWidth;
		private float timeHeight;
		private float jieshuWidth;
		private float jieshuHeight;
		
		public void init() {
			
			cellHeight = height/10f;
			cellWidth = (width / 3) * 2;
			jieshuHeight = cellHeight;
			jieshuWidth = ((width - cellWidth)/3) * 2;
			timeWidth = width - cellWidth - jieshuWidth;
			timeHeight = 4 * cellHeight;
			
			borderPaint = new Paint();
			borderPaint.setColor(borderColor);
			borderPaint.setStyle(Paint.Style.STROKE);
			borderWidth = (float) (0.5 * density);
			borderWidth = borderWidth < 1 ? 1 : borderWidth;
			borderPaint.setStrokeWidth(borderWidth);
			
			TimePaint = new Paint();
			TimePaint.setColor(textColor);
			TimePaint.setAntiAlias(true);
			float timeTextSize = timeWidth* 0.8f;
			TimePaint.setTextSize(timeTextSize);
			TimePaint.setTextAlign(Align.LEFT);
			TimePaint.setTypeface(Typeface.DEFAULT_BOLD);
			
			jieshuPaint = new Paint();
			jieshuPaint.setColor(textColor);
			jieshuPaint.setAntiAlias(true);
			float jieshuTextSize = jieshuWidth * 0.8f;
			jieshuPaint.setTextSize(jieshuTextSize);
			jieshuPaint.setTypeface(Typeface.DEFAULT_BOLD);
			
			boxPath = new Path();
			boxPath.rLineTo(width, 0);
			for (int i = 1; i < 3; i++) {
				for(int j = 1; j < 4; j++) {
					boxPath.moveTo(timeWidth,  cellHeight * j + cellHeight * (i-1) * 4);
					boxPath.rLineTo(width, 0);
				}
				boxPath.moveTo(0,  4 * cellHeight * i);
				boxPath.rLineTo(width, 0);
			}
			for(int i = 0; i < 3; i++) {
				boxPath.moveTo(timeWidth, cellHeight * i + cellHeight * 8);
				boxPath.rLineTo(width, 0);
			}
			boxPath.moveTo(0, 8 * cellHeight);
			boxPath.rLineTo(width, 0);
			
			boxPath.moveTo(timeWidth, 0);
			boxPath.rLineTo(0,height);
			
			boxPath.moveTo(timeWidth+jieshuWidth, 0);
			boxPath.rLineTo(0,height);

		}
	}




	
	interface OnItemClickListener {
		void onItemClick();
	}





	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return false;
	}
}
